﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAGame_Lab1
{
    public class MyXNAStore : MyXNAButton
    {
        public event EventHandler Clicked;
        public MyXNAStore(ContentManager content, List<string> strTexture, int nSprites, Vector2 topleft, Vector2 size) :
            base(content, strTexture, nSprites,  topleft,  size) { }
        public MyXNAStore(ContentManager content, string refix, int nSprites, Vector2 topleft, Vector2 size):base(content,refix,nSprites,topleft,size)
        {

        }
        // Gets fired when the button is held down.
        public event EventHandler OnPress;
        
                public enum ButtonStatus
        {
            not,
            Normal,
            MouseOver,
            Pressed,
            attack,
        }
        private MouseState previousState;

        public MouseState PreviousState
        {
            get { return previousState; }
            set { previousState = value; }
        }
        public static ButtonStatus state = ButtonStatus.Normal;

        public ButtonStatus State
        {
            get { return state; }
            set { state = value; }
        }
        
        // Gets fired when the button is pressed.
        bool isMouseOver;

        public override void Update(GameTime gameTime)
        {
            MouseState mouseState = Mouse.GetState();
            if (mouseState.LeftButton==ButtonState.Pressed&& this.isClicked(new Vector2(mouseState.X, mouseState.Y)))
                isMouseOver = true;

            if (mouseState.LeftButton == ButtonState.Pressed &&
previousState.LeftButton == ButtonState.Released)
            {
                if (isMouseOver == true)
                {
                    // Update the button state.
                    state = ButtonStatus.Pressed;
                }
                if (OnPress != null)
                {
                    // Fire the OnPress event.
                    OnPress(this, EventArgs.Empty);
                }
            }
            if (mouseState.LeftButton == ButtonState.Released &&
                previousState.LeftButton == ButtonState.Pressed)
            {
                if (isMouseOver == true)
                {
                    // update the button state.
                    state = ButtonStatus.MouseOver;

                    if (Clicked != null)
                    {
                        // Fire the clicked event.
                        Clicked(this, EventArgs.Empty);
                    }
                }

                else if (state == ButtonStatus.Pressed)
                {
                    state = ButtonStatus.Normal;
                }
                isMouseOver = false;
            }
            previousState = mouseState;
            
        
            base.Update(gameTime);
        }
      
    }
    
}
